﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using EPIClientRssWP7.WP7ViewModel;
using GalaSoft.MvvmLight.Messaging;
using System.Collections.Specialized;

namespace EPIClientRssWP7
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        static public int lancement = 0;

        public WP7AuthenticationViewModel Vm
        { get; set; }
        public MainPage()
        {
            InitializeComponent();
            this.DataContext = Vm = new WP7AuthenticationViewModel();
            Loaded += (obj, arg) =>
            {
//				WP7AuthenticationViewModel.RaiseAuthenticationNavEvent += new EventHandler<AuthenticationNavEventArg>(Vm_RaiseAuthenticationNavEvent);
                if (lancement == 0)
                {

                    Vm.DoesNeedAuthentication();
                    lancement++;
                }
                else 
                {
                    authView.IsEnabled = true;
                    authView.Opacity = 1.0;
                    while (NavigationService.CanGoBack)
                        NavigationService.RemoveBackEntry();
                }
            };
			
			Messenger.Default.Register<string>(this, "AuthenticationSwitch" ,(res) => {
				if (res == "Home") {
					NavigationService.Navigate(new Uri("/View/MasterHomeView.xaml", UriKind.RelativeOrAbsolute));
					authView.IsEnabled = false;
					authView.Opacity = 0.0;
				}
				else if (res == "Auth") {
					authView.IsEnabled = true;
					authView.Opacity = 1.0;
                    //while (NavigationService.CanGoBack)
                    //    NavigationService.RemoveBackEntry();
				}
			});
        }
    }
}